﻿using Ini.Net;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Lottery
{
    public class MyConfig
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static MyConfig instance = null;

        private IniFile iniFile = null;


        public static MyConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MyConfig();
                    instance.Load();
                }
                return MyConfig.instance;
            }
        }

        private MyConfig()
        {
            this.iniFile = new IniFile(MyResources.GetConfigPath(MyResources.ConfigName));
        }

        private String appTitle;

        public String AppTitle
        {
            get { return appTitle; }
            set { appTitle = value; }
        }

        private int timeClose;

        public int TimeClose
        {
            get { return timeClose; }
            set { timeClose = value; }
        }

        private String messageBar;

        public String MessageBar
        {
            get { return messageBar; }
            set { messageBar = value; }
        }

        private List<String> soundList;

        public List<String> SoundList
        {
            get { return soundList; }
            set { soundList = value; }
        }

        private Queue<String> soundQueue = new Queue<string>();


        public Queue<String> SoundQueue
        {
            get
            {
                if (soundQueue.Count <= 0)
                {
                    foreach (String x in MyConfig.Instance.SoundList)
                        this.soundQueue.Enqueue(x);
                }
                return soundQueue;
            }
        }

        private void Load()
        {
            try
            {
                String group = "基本设置";
                this.appTitle = this.iniFile.ReadString(group, "程序名");
                this.messageBar = this.iniFile.ReadString(group, "滚动栏");
                this.timeClose = this.iniFile.ReadInteger(group, "弹窗时间");
                IDictionary<String, String> list2 = this.iniFile.ReadSection("背景音乐列表");
                this.soundList = list2.Values.ToList<String>();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw new Exception("配置文件错误,请仔细常查看配置说明！");
            }
        }

        public void Save()
        {
            try
            {
                String group = "基本设置";
                this.iniFile.WriteString(group, "程序名", this.appTitle);
                this.iniFile.WriteString(group, "滚动栏", this.messageBar);
                this.iniFile.WriteInteger(group, "弹窗时间", this.timeClose);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw new Exception("配置文件错误,请仔细常查看配置说明！");
            }
        }

    }
}
